'use strict';

String.prototype.format = function () {
    for (var _len = arguments.length, arg = Array(_len), _key = 0; _key < _len; _key++) {
        arg[_key] = arguments[_key];
    }

    if (arg.length === 0) {
        return this;
    }
    var s = this;
    if (Array.isArray(arg[0])) {
        arg = arg[0];
    }
    for (var i = 0; i < arg.length; i++) {
        s = s.replace(new RegExp("\\{" + i + "\\}", "g"), arg[i]);
    }
    return s;
};
var weui = {
    alert: function alert(msg) {
        var title = arguments.length <= 1 || arguments[1] === undefined ? "提示信息" : arguments[1];
        var callback = arguments[2];

        var alertHtml = '',
            weui_alert = $(".weui_dialog_alert");
        alertHtml = '<div class="weui_dialog_alert" style="display: none;">' + '<div class="weui_mask"></div>' + '<div class="weui_dialog">' + '    <div class="weui_dialog_hd"><strong class="weui_dialog_title">' + title + '</strong></div>' + '    <div class="weui_dialog_bd"></div>' + '    <div class="weui_dialog_ft">' + '      <a href="javascript:;" class="weui_btn_dialog primary">确定</a>' + '  </div>' + ' </div>' + '</div>';
        if ($(".weui_dialog_alert").length > 0) {
            $(".weui_dialog_alert .weui_dialog_bd").empty();
        } else {
            $("body").append($(alertHtml));
        }
        weui_alert.show();
        weui_alert.find(".weui_dialog_bd").html(msg);
        weui_alert.find('.weui_btn_dialog').off("click").on('click', function () {
            weui_alert.hide();
            if (callback) {
                callback();
            }
        });
    },
    confirm: function confirm(msg, title, callback) {
        var weui_confirm = $(".weui_dialog_confirm"),
            confirmHtml = '<div class="weui_dialog_confirm">' + '<div class="weui_mask"></div>' + '<div class="weui_dialog">' + '<div class="weui_dialog_hd"><strong class="weui_dialog_title"></strong></div>' + '<div class="weui_dialog_bd"></div>' + '<div class="weui_dialog_ft">' + '<a href="javascript:;" class="weui_btn_dialog default">取消</a>' + '<a href="javascript:;" class="weui_btn_dialog primary">确定</a>' + '</div>' + '</div>' + '</div>';
        if ($(".weui_dialog_confirm").length > 0) {
            $(".weui_dialog_confirm .weui_dialog_bd").empty();
        } else {
            $("body").append($(confirmHtml));
        }
        weui_confirm.show();
        weui_confirm.find(".weui_dialog_title").text(title = title ? title : "确认提示");
        weui_confirm.find(".weui_dialog_bd").html(msg);
        weui_confirm.find('.primary').off("click").on('click', function () {
            weui_confirm.hide();
            if (callback) {
                callback(true);
            }
        });
        weui_confirm.find('.default').off("click").on('click', function () {
            weui_confirm.hide();
            if (callback) {
                callback(false);
            }
        });
    },

    destroyFlag: false,
    Loading: {
        show: function show() {
            var msg = arguments.length <= 0 || arguments[0] === undefined ? "处理中.." : arguments[0];

            var weui_loading = $(".weui_loading_toast"),
                loadingHtml = '<div  class="weui_loading_toast" style="display: none;">' + '<div class="weui_mask_transparent"></div>' + '<div class="weui_toast">' + '<div class="weui_loading">' + ' <div class="weui_loading_leaf weui_loading_leaf_0"></div>' + ' <div class="weui_loading_leaf weui_loading_leaf_1"></div>' + ' <div class="weui_loading_leaf weui_loading_leaf_2"></div>' + ' <div class="weui_loading_leaf weui_loading_leaf_3"></div>' + ' <div class="weui_loading_leaf weui_loading_leaf_4"></div>' + ' <div class="weui_loading_leaf weui_loading_leaf_5"></div>' + ' <div class="weui_loading_leaf weui_loading_leaf_6"></div>' + ' <div class="weui_loading_leaf weui_loading_leaf_7"></div>' + ' <div class="weui_loading_leaf weui_loading_leaf_8"></div>' + ' <div class="weui_loading_leaf weui_loading_leaf_9"></div>' + ' <div class="weui_loading_leaf weui_loading_leaf_10"></div>' + ' <div class="weui_loading_leaf weui_loading_leaf_11"></div>' + '</div>' + '<p class="weui_toast_content"></p>' + '</div>' + '</div>',
                width = 0,
                w = 0;
            if ($(".weui_loading_toast").length > 0) {
                $(".weui_loading_toast .weui_toast_content").empty();
            } else {
                $("body").append($(loadingHtml));
            }
            weui_loading.find(".weui_toast_content").text(msg).end().show();
            width = weui_loading.find(".weui_toast").width();
            w = $(document).width();
            weui_loading.find(".weui_toast").css("left", (w - width) / 2);
        },
        hide: function hide() {
            if (!weui.destroyFlag) $(".weui_loading_toast").hide();
        },
        msg: function msg(_msg, time, cls, icon) {
            var successHtml = '<div class="' + cls + '" style="display: none;">' + '<div class="weui_mask_transparent"></div>' + '<div class="weui_toast">' + '<i class="' + icon + '"></i>' + '<p class="weui_toast_content"></p>' + '</div>' + '</div>',
                t = 3000;
            if ($("." + cls).length < 1) {
                $("body").append($(successHtml));
                $(".weui_mask_transparent").off("click").on("click", function () {
                    $("." + cls).hide();
                });
            }
            if (time) {
                t = time;
            }
            var weui_success = $("." + cls).find(".weui_toast_content").text(_msg).end();
            weui_success.show();
            var width = weui_success.find(".weui_toast").width();
            var w = $(document).width();
            weui_success.find(".weui_toast").css("left", (w - width) / 2 - 5);
            setTimeout(function () {
                weui_success.hide();
            }, t);
        },
        success: function success(time) {
            this.msg("操作成功", time, "weui_success_toast", "weui_icon_toast");
        },
        info: function info(msg, time) {
            this.msg(msg, time, "weui_info_toast", "weui_icon_info");
        },
        error: function error(msg, time) {
            this.msg(msg, time, "weui_error_toast", "weui_icon_warn");
        }
    },
    actionSheet: {
        /**
         * 生成弹出选项
         * @param cells 选项列表集合 如:[{name:"选项1",value:"1"}];
         * @param change 选中事件回调函数,通过返回true可以改变默认选中隐藏操作
         * @param sington 启用单列模式
         * @returns actionSheet本身
         */

        create: function create(cells, change, sington) {
            var _this = this;

            var item = '<div class="weui_actionsheet_cell" data-value="{1}">{0}</div>',
                sheet_div = '<div id="actionSheet_wrap_{0}">' + '<div class="weui_mask_transition" id="mask"></div>' + '<div class="weui_actionsheet" id="weui_actionsheet">' + '<div class="weui_actionsheet_menu">' + '</div>' + '<div class="weui_actionsheet_action">' + '<div class="weui_actionsheet_cell" id="actionsheet_cancel">取消</div>' + '</div>' + '</div>' + '</div>',
                index = $("div[id^='actionSheet_wrap_']").length,
                iscreate = true,
                _self = this,
                sheet = null,
                array = [];
            if (sington) {
                iscreate = index == 0;
                index = 0;
            }
            if (iscreate) {
                $("body").append(sheet_div.format(index));
                sheet = $("#actionSheet_wrap_" + index);
                sheet.find("#actionsheet_cancel").on("click", function () {
                    _this.hide(); //外层this？
                });
            }
            sheet = $("#actionSheet_wrap_" + index);
            sheet.find(".weui_actionsheet_menu").empty();
            $.each(cells, function () {
                if (!this.name || !this.value) {
                    throw new Error("cells array style [{name:'name',value:'value'}]! ");
                }
                array.push(item.format(this.name, this.value));
            });
            sheet.find(".weui_actionsheet_menu").append(array.join("")).find(".weui_actionsheet_cell").on("click", function () {
                var cell = $(this),
                    is_hide = change(cell.data("value"), cell.text(), cell);
                if (!is_hide) {
                    _self.hide();
                }
            });
            this._index = index;
            return _self;
        },

        _index: 0,
        show: function show() {
            var _this2 = this;

            $("#actionSheet_wrap_" + this._index).find("#mask").addClass("weui_fade_toggle").show().one("click", function () {
                _this2.hide(); //外层this？
            }).end().find("#weui_actionsheet").addClass("weui_actionsheet_toggle");
            return this;
        },
        hide: function hide() {
            $("#actionSheet_wrap_" + this._index).find("#mask").removeClass("weui_fade_toggle").hide().end().find("#weui_actionsheet").removeClass("weui_actionsheet_toggle");
            return this;
        },
        destroy: function destroy() {
            $("#actionSheet_wrap_" + this._index).remove();
        }
    },
    destroy: function destroy() {
        $(".weui_dialog_alert,.weui_dialog_confirm,.weui_loading_toast,.weui_success_toast,.weui_info_toast,.weui_error_toast,.weui_actionsheet_cell").remove();
        weui.destroyFlag = true;
    }
};